// 2025/10/10: 首个版本
// 2025/11/12: 优化tab control显示

using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class CheckReferences : DialogPanel
    {
        public CheckReferences()
        {
            InitializeComponent();
            SetResizableMode(600, 200, 1000, 500);
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
            Title = language["title"];

            if (AgencyLocal.ClientSide)
            {
                tabHost.Header = language["remote"];
            }
            else
            {
                tabHost.Header = language["local"];
                tabClient.IsVisible = false;
                tabControl.SelectedIndex = 1;
            }

            if (FuncManager.Run("System.GetDesignerModeFlag") == null)
            {
                initialize();
            }
        }

        public String IconResourceName
        {
            set => Icon = ResourceLoader.Load(value)?.ToAvaloniaWindowIcon();
        }

        private async void initialize()
        {
            if (AgencyLocal.ClientSide)
            {
                var localRef = AgencyLocal.GetDataReferenceInfo();
                setBusInfo(textLocalBus, localRef.AlwaysTransferBusMessage, localRef.BusMessageReferences);
                setPointInfo(textLocalPoint, localRef.AlwaysTransferPointCloud, localRef.PointCloudReferences);
                setSignalInfo(textLocalSignal, localRef.AlwaysTransferSignal, localRef.SignalReferences);
                setSubscriberInfo(textLocalSubscriber, localRef.SubscriberReferences);
            }

            var hostRef = await AgencyAsync.GetDataReferenceInfo();
            if (hostRef != null)
            {
                setBusInfo(textBus, hostRef.AlwaysTransferBusMessage, hostRef.BusMessageReferences);
                setVideoInfo(textVideo, hostRef.AlwaysTransferProcessorVideo, hostRef.ProcessorVideoReferences);
                setPointInfo(textPoint, hostRef.AlwaysTransferPointCloud, hostRef.PointCloudReferences);
                setSignalInfo(textSignal, hostRef.AlwaysTransferSignal, hostRef.SignalReferences);
                setSubscriberInfo(textSubscriber, hostRef.SubscriberReferences);
            }
            else
            {
                textBus.Text = language["failed"];
                textVideo.Text = language["failed"];
                textPoint.Text = language["failed"];
                textSignal.Text = language["failed"];
                textSubscriber.Text = language["failed"];
            }
        }

        private void setBusInfo(TextBox target, bool always, int[] references)
        {
            if (always)
            {
                target.Text = language["always"];
                return;
            }

            var texts = new List<String>();
            for (int i = 0; i < references.Length; i++)
            {
                texts.Add("CH" + (i + 1) + " (" + references[i] + ")");
            }
            target.Text = String.Join('\n', texts);
        }

        private void setVideoInfo(TextBox target, bool always, int[] references)
        {
            if (always)
            {
                target.Text = language["always"];
                return;
            }

            var texts = new List<String>();
            for (int i = 0; i < references.Length; i++)
            {
                texts.Add((char)('A' + i) + " (" + references[i] + ")");
            }
            target.Text = String.Join('\n', texts);
        }

        private void setPointInfo(TextBox target, bool always, int[] references)
        {
            if (always)
            {
                target.Text = language["always"];
                return;
            }

            var texts = new List<String>();
            for (int i = 0; i < references.Length; i++)
            {
                texts.Add((char)('A' + i) + " (" + references[i] + ")");
            }
            target.Text = String.Join('\n', texts);
        }

        private void setSignalInfo(TextBox target, bool always, Dictionary<String, int> references)
        {
            if (always)
            {
                target.Text = language["always"];
                return;
            }
            if (references.Count == 0)
            {
                target.Text = language["none"];
                return;
            }

            var texts = new List<String>();
            foreach (var pair in references)
            {
                texts.Add(pair.Key + " (" + pair.Value + ")");
            }
            target.Text = String.Join('\n', texts);
        }

        private void setSubscriberInfo(TextBox target, Dictionary<String, int> references)
        {
            if (references.Count == 0)
            {
                target.Text = language["none"];
                return;
            }

            var texts = new List<String>();
            foreach (var pair in references)
            {
                texts.Add(pair.Key + " (" + pair.Value + ")");
            }
            target.Text = String.Join('\n', texts);
        }

        private LanguageSwitch language;
    }
}